package net.lab1024.sa.admin.module.yitihua;


import com.alibaba.fastjson.JSONObject;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.cert.X509Certificate;


public class HttpUtil {

	public static void trustAllHosts() {

		// Create a trust manager that does not validate certificate chains
		TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

			public X509Certificate[] getAcceptedIssuers() {
				return new X509Certificate[] {};
			}

			public void checkClientTrusted(X509Certificate[] chain, String authType) {

			}

			public void checkServerTrusted(X509Certificate[] chain, String authType) {

			}
		} };

		// Install the all-trusting trust manager
		try {
			SSLContext sc = SSLContext.getInstance("TLS");
			sc.init(null, trustAllCerts, new java.security.SecureRandom());
			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * url编码
	 * @param text
	 * @return
	 */
	public static String encode(String text) {
		String result = "";
		try {
			result = URLEncoder.encode(text, "UTF-8");
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}

	public static JSONObject httpGet(String url, JSONObject header) throws Exception {

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("responseString", "");
		jsonObject.put("responseCode", 200);
		trustAllHosts();
		String s = "";
		HttpURLConnection urlCon = null;
		InputStream is = null;
		InputStreamReader isr = null;
		BufferedReader bufferReader = null;
		OutputStreamWriter out = null;
		try {
			URL u = new URL(url);
			/*try {
				ignoreSsl();
			} catch (Exception e) {
				e.printStackTrace();
			}*/
			// 获取http对象
			urlCon = (HttpURLConnection) u.openConnection();
			// 设置输入
			urlCon.setDoInput(true);
			// 设置输出
			urlCon.setDoOutput(true);
			urlCon.setRequestMethod("GET");
			// 设置不使用缓存
			urlCon.setUseCaches(false);
			// 设置传送的内容为可序列化的java对象
			if (header != null) {
				for (Object s1 : header.keySet()) {
					String key = (String) s1;
					urlCon.setRequestProperty(key, header.getString(key));
				}
			}
			urlCon.connect();
			Reader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), "UTF-8"));
			StringBuilder sb = new StringBuilder();
			for (int c; (c = in.read()) >= 0;) {
				sb.append((char) c);
			}
			s = sb.toString();
		} catch (Exception e) {
			throw e;
		} finally {
			if (bufferReader != null) {
				bufferReader.close();
				bufferReader = null;
			}
			if (isr != null) {
				isr.close();
				isr = null;
			}
			if (is != null) {
				is.close();
				is = null;
			}
			if (urlCon != null) {
				jsonObject.put("responseCode", urlCon.getResponseCode());
				jsonObject.put("responseString", s);
				urlCon.disconnect();
				urlCon = null;
			}
		}
		return jsonObject;
	}
}
